package com.future.dao;

import java.sql.Connection;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import com.future.model.po.OfficeServicesModel;
import com.future.model.po.UserModel;

public class OfficeServicesDAO extends BaseDAO<OfficeServicesModel> {
	private static Logger logger = LogManager.getLogger(OfficeServicesDAO.class);

	public OfficeServicesDAO() {

	}

	public OfficeServicesDAO(Connection conn) {
		super(conn);
	}

	public boolean save(OfficeServicesModel model) {
		String sql = "insert into officeservices (officeid,services,issupport) values (?,?,?)";
		return this.update(sql, model.getOfficeid(), model.getServices(), model.getIssupport());
	}

	public List<OfficeServicesModel> findAll() {
		String sql = "select * from officeservices";
		return this.searchAsBeanList(sql, OfficeServicesModel.class);
	}

	public List<OfficeServicesModel> findByOfficeId(Integer officeId) {
		OfficeServicesModel model = new OfficeServicesModel();
		model.setOfficeid(officeId);
		return findByOfficeId(model);
	}

	public List<OfficeServicesModel> findByOfficeId(OfficeServicesModel model) {
		String sql = "select * from officeservices where officeid = ? ";
		return this.searchAsBeanList(sql, OfficeServicesModel.class, model.getOfficeid());
	}

	public List<OfficeServicesModel> find(Map<String, Object> condition) {
		StringBuffer sb = new StringBuffer("select * from officeservices where ");
		List<Object> values = new ArrayList<Object>();
		Iterator<String> columns = condition.keySet().iterator();
		while (columns.hasNext()) {
			String column = columns.next();
			sb.append(column + " = ? ");
			values.add(condition.get(column));
			if (columns.hasNext()) {
				sb.append(" and ");
			}
		}
		Object[] params = values.toArray();
		String sql = sb.toString();
		logger.debug(sql);
		logger.debug(params);
		return this.searchAsBeanList(sql, OfficeServicesModel.class, params);
	}
	
	/**
	 * 判断是否添加过 处室业务
	 * @param model
	 * @return  true 表示当前用户添加过   false 表示当前用户没有添加过
	 */
	public boolean isOrExist(UserModel model) {
		String sql = "select * from officeservices where officeid = ? ";
		List<OfficeServicesModel> list = this.searchAsBeanList(sql, OfficeServicesModel.class,
				model.getId());
		if (list != null && list.size() > 0) {
			return true;
		} else {
			return false;
		}
	}
}
